﻿using System;
using System.Collections.Generic;
using DataCenter.Interface.Model;
using System.Text;

namespace DataCenter.Component
{
    public class TimeRegion
    {
        public TimeRegion(DateTime time0, DateTime time1)
        {
            this.time0 = time0;
            this.time1 = time1;
            this.dictObject = new Dictionary<int, ObjectEntity>();
        }
        public TimeRegion(DateTime time0, DateTime time1, ObjectEntity objectEntity)
            :this(time0, time1)
        {
            this.dictObject.Add(objectEntity.ObjectId, objectEntity);
        }

        private DateTime time0;
        private DateTime time1;
        private Dictionary<int, ObjectEntity> dictObject;

        public DateTime Time0
        {
            get { return time0; }
            set { time0 = value; }
        }

        public DateTime Time1
        {
            get { return time1; }
            set { time1 = value; }
        }

        public Dictionary<int, ObjectEntity> DictObject
        {
            get { return dictObject; }
            set { dictObject = value; }
        }

        public int ObjectCount
        {
            get { return dictObject.Count; }
        }

        public bool IsInRegion(DateTime time, int objectId)
        {
            if(time >= time0 && time <= time1)
                return dictObject.ContainsKey(objectId);
            return false;
        }

        public void AddObject(ObjectEntity objectEntity)
        {
            int objectId = objectEntity.ObjectId;
            if (!dictObject.ContainsKey(objectId))
                dictObject.Add(objectId, objectEntity);
        }

        public void RemoveObject(int objectId)
        {
            if (dictObject.ContainsKey(objectId))
                dictObject.Remove(objectId);
        }

        public void CombinObject(TimeRegion region)
        {
            foreach (KeyValuePair<int, ObjectEntity> pair in region.dictObject)
            {
                if (!dictObject.ContainsKey(pair.Key))
                    dictObject.Add(pair.Key, pair.Value);
            }
        }

        public override string ToString()
        {
            return time0 + "~" + time1 + "(" + dictObject.Count + ")";
        }
    }
}
